<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'

import { ref } from 'vue'
import '@/assets/css/tagStyle.css'
import '@/assets/css/fancySearchForm.css'
//客户数据模型
const clients = ref([
    {
        "id": 3,
        "name": "美食",
        "contactPerson": "my",
        "contactPhone": "11111111111",
        "email": "11111111111",
        "address": "111",
        "state": "1",
        "contractStart": "2024-01-01",
        "contractEnd": "2024-12-31",
        "createTime": "2023-09-02 12:06:59",
        "updateTime": "2023-09-02 12:06:59"
    },
    {
        "id": 4,
        "name": "11",
        "contactPerson": "my",
        "contactPhone": "11111111111",
        "email": "11111111111",
        "address": "111",
        "state": "1",
        "contractStart": "2024-01-01",
        "contractEnd": "2024-12-31",
        "createTime": "2023-09-02 12:06:59",
        "updateTime": "2023-09-02 12:06:59"
    },
    {
        "id": 5,
        "name": "22",
        "contactPerson": "my",
        "contactPhone": "11111111111",
        "email": "11111111111",
        "address": "111",
        "state": "1",
        "contractStart": "2024-01-01",
        "contractEnd": "2024-12-31",
        "createTime": "2023-09-02 12:06:59",
        "updateTime": "2023-09-02 12:06:59"
    }
])

//用户搜索时选中的分类id
const clientId=ref('')

const title = ref('')

//用户搜索时选中的发布状态
const state=ref('')

//广告位数据模型
const adpositions = ref([
    {
        "id": 5,
        "clientId": 6,
        "positionName": "阿里巴巴大促",
        "positionCode": "1",
        "positionType": 3,
        "dimensions": "200x100",
        "price": 1000,
        "state": "1",
        "scheduledStart": "2024-01-01",
        "scheduledEnd": "2024-12-31",
        "createTime": "2023-09-03 11:55:30",
        "updateTime": "2023-09-03 11:55:30"
    },
    {
        "id": 5,
        "clientId": 6,
        "positionName": "阿里巴巴大促",
        "positionCode": "1",
        "positionType": 3,
        "dimensions": "200x100",
        "price": 1000,
        "state": "1",
        "scheduledStart": "2024-01-01",
        "scheduledEnd": "2024-12-31",
        "createTime": "2023-09-03 11:55:30",
        "updateTime": "2023-09-03 11:55:30"
    },
    {
        "id": 5,
        "clientId": 6,
        "positionName": "阿里巴巴大促",
        "positionCode": "1",
        "positionType": 3,
        "dimensions": "200x100",
        "price": 1000,
        "state": "1",
        "scheduledStart": "2024-01-01",
        "scheduledEnd": "2024-12-31",
        "createTime": "2023-09-03 11:55:30",
        "updateTime": "2023-09-03 11:55:30"
    },
])

//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(5)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size
    adpositionList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num
    adpositionList()
}


//客户信息回显
import {clientListService,adpositionListService,adpositionAddService, adpositionUpdateService,adpositionDeleteServcie} from '@/api/adposition.js'
const clientList = async ()=>{
    let result = await clientListService();
    clients.value = result.data;
    console.log(Array.isArray(result.data))
    // console.log(clients.value[0].name);

}

const adpositionList = async ()=>{
    let params = {
        pageNum:pageNum.value,
        pageSize:pageSize.value,
        clientId:clientId.value ? clientId.value : null
    }

    let result = await adpositionListService(params)

    total.value = result.data.total
    adpositions.value = result.data.items

    for(let i=0;i<adpositions.value.length;i++){
        let adposition = adpositions.value[i];
        for(let j = 0;j<clients.value.length;j++){
            switch(adposition.positionType){
                case 1:adposition.positionTypeName = "Banner";break;
                case 2:adposition.positionTypeName = "侧边栏";break;
                case 3:adposition.positionTypeName = "弹窗";break;
                case 4:adposition.positionTypeName = "信息流"
            }
            switch(adposition.state){
                case "1":adposition.stateName = "已付款";break;
                case "0":adposition.stateName = "未付款";break;
                default: adposition.stateName = "未付款";
            }

            if(adposition.clientId == clients.value[j].id){
                adposition.clientName = clients.value[j].name
            }
        }
    }
}

clientList();
adpositionList()

import {Plus} from '@element-plus/icons-vue'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const adpositionModel = ref({
    clientId:'',
    positionName:'',
    positionType:'',
    dimensions: '',
    price: '',
    state: '',
    scheduledStart: '',
    scheduledEnd: '',
})

import { ElMessage } from 'element-plus'
const addadposition = async ()=>{
    console.log(adpositionModel.value.positionName,adpositionModel.value.positionType)

    let result = await adpositionAddService(adpositionModel.value);

    ElMessage.success(result.msg ? result.msg : '添加成功')

    visibleDrawer.value = false

    adpositionList()
}

const clearData = ()=>{
    visibleDrawer.value=true;
    title.value='添加广告';
    adpositionModel.value.clientId = '';
    adpositionModel.value.positionName = '';
    adpositionModel.value.positionType = '';
    adpositionModel.value.dimensions = '';
    adpositionModel.value.price = '';
    adpositionModel.value.state = '';
    adpositionModel.value.scheduledStart = '';
    adpositionModel.value.scheduledEnd = '';
}

const showDialog = (row)=>{
    visibleDrawer.value=true;
    title.value='编辑广告';
    adpositionModel.value.clientId = row.clientId;
    adpositionModel.value.positionName = row.positionName;
    adpositionModel.value.positionType = row.positionType;
    adpositionModel.value.dimensions = row.dimensions;
    adpositionModel.value.price = row.price;
    adpositionModel.value.state = row.state;
    adpositionModel.value.scheduledStart = row.scheduledStart;
    adpositionModel.value.scheduledEnd = row.scheduledEnd;
    adpositionModel.value.id = row.id;
}

const updateAdpostion = async()=>{
    // articleModel.value.state = clickState;
    let result = await adpositionUpdateService(adpositionModel.value);
    ElMessage.success(result.msg? result.msg:'编辑成功')
    adpositionList();
    visibleDrawer.value=false;
}

import { ElMessageBox } from 'element-plus';
const deleteAdposition = (row)=>{
    ElMessageBox.confirm(
    '你确定要删除该广告吗?',
    '温馨提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
      let result = await adpositionDeleteServcie(row.id)
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      adpositionList();
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消操作',
      })
    })
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>广告位管理</span>
                <div class="extra">
                    <el-button type="primary" @click="clearData()">添加广告</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
<el-form inline class="fancy-search-form">
  <el-form-item label="客户：">
    <el-select
      placeholder="请选择"
      v-model="clientId"
      style="width: 160px"
    >
      <el-option
        v-for="c in clients"
        :key="c.id"
        :label="c.name"
        :value="c.id"
      />
    </el-select>
  </el-form-item>

  <el-form-item>
    <el-button type="primary" @click="adpositionList" class="fancy-btn confirm">搜索</el-button>
    <el-button @click="clientId=''" class="fancy-btn cancel">重置</el-button>
  </el-form-item>


        </el-form>
        <!-- 文章列表 -->
  <el-table :data="adpositions" style="width: 100%" class="custom-table" border highlight-current-row>
    <!-- 圆形序号 -->
    <el-table-column label="序号" width="60" type="index">
      <template #default="{ $index }">
        <span class="tag tag-blue">{{ $index + 1 }}</span>
      </template>
    </el-table-column>

    <!-- 内容字段加彩色 tag -->
    <el-table-column label="广告位名称" prop="positionName" width="180px" >
      <template #default="{ row }">
        <span class="tag tag-blue">{{ row.positionName }}</span>
      </template>
    </el-table-column>

    <el-table-column label="客户名称" prop="clientName"  >
      <template #default="{ row }">
        <span class="tag tag-orange">{{ row.clientName }}</span>
      </template>
    </el-table-column>

    <el-table-column label="广告位类型" prop="positionTypeName">
      <template #default="{ row }">
        <span class="tag tag-purple">{{ row.positionTypeName }}</span>
      </template>
    </el-table-column>

    <el-table-column label="广告尺寸" prop="dimensions"  >
      <template #default="{ row }">
        <span class="tag tag-cyan">{{ row.dimensions }}</span>
      </template>
    </el-table-column>

    <el-table-column label="广告位单价" prop="price" >
      <template #default="{ row }">
        <span class="tag tag-pink">{{ row.price }}</span>
      </template>
    </el-table-column>

    <!-- 状态字段 -->
    <el-table-column label="状态" prop="stateName"  width="100px">
      <template #default="{ row }">
        <span class="tag" :class="row.stateName === '已发布' ? 'tag-green' : 'tag-gray'">
          {{ row.stateName }}
        </span>
      </template>
    </el-table-column>

    <el-table-column label="排期开始" prop="scheduledStart">
      <template #default="{ row }">
        <span class="tag tag-teal">{{ row.scheduledStart }}</span>
      </template>
    </el-table-column>

    <el-table-column label="排期结束" prop="scheduledEnd">
      <template #default="{ row }">
        <span class="tag tag-teal">{{ row.scheduledEnd }}</span>
      </template>
    </el-table-column>

    <!-- 操作按钮 -->
    <el-table-column label="操作" width="200">
      <template #default="{ row }">
        <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)" />
        <el-button :icon="Delete" circle plain type="danger" @click="deleteAdposition(row)" />
      </template>
    </el-table-column>

    <template #empty>
      <el-empty description="没有数据" />
    </template>
  </el-table>


        <!-- 分页条 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[5 ,10, 15]"
            layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
            @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
        
          <el-drawer
           v-model="visibleDrawer"
            :title="title"
             direction="rtl"
             size="50%"
              class="fancy-dialog"
>
  <el-form :model="adpositionModel" label-width="100px" class="fancy-form">

    <el-form-item label="广告位名称" class="fancy-input">
      <el-input v-model="adpositionModel.positionName" placeholder="请输入名称" />
    </el-form-item>

    <el-form-item label="客户名称" class="fancy-input">
      <el-select v-model="adpositionModel.clientId" placeholder="请选择">
        <el-option
          v-for="c in clients"
          :key="c.id"
          :label="c.name"
          :value="c.id"
        />
      </el-select>
    </el-form-item>

    <el-form-item label="广告位类型" class="fancy-input">
      <el-select v-model="adpositionModel.positionType" placeholder="请选择类型">
        <el-option label="Banner" value="1" />
        <el-option label="侧边栏" value="2" />
        <el-option label="弹窗" value="3" />
        <el-option label="信息流" value="4" />
      </el-select>
    </el-form-item>

    <el-form-item label="广告位尺寸" class="fancy-input">
      <el-input v-model="adpositionModel.dimensions" placeholder="请输入尺寸" />
    </el-form-item>

    <el-form-item label="广告位价格" class="fancy-input">
      <el-input v-model="adpositionModel.price" placeholder="请输入价格" />
    </el-form-item>

    <el-form-item label="广告位状态" class="fancy-input">
      <el-select v-model="adpositionModel.state" placeholder="请选择类型">
        <el-option label="已付款" value="1" />
        <el-option label="未付款" value="0" />
      </el-select>
    </el-form-item>

    <el-form-item label="排期开始" class="fancy-input">
      <el-date-picker
        v-model="adpositionModel.scheduledStart"
        type="date"
        placeholder="请选择开始日期"
        format="YYYY-MM-DD"
        value-format="YYYY-MM-DD"
        style="width: 100%"
      />
    </el-form-item>

    <el-form-item label="排期结束" class="fancy-input">
      <el-date-picker
        v-model="adpositionModel.scheduledEnd"
        type="date"
        placeholder="请选择结束日期"
        format="YYYY-MM-DD"
        value-format="YYYY-MM-DD"
        style="width: 100%"
      />
    </el-form-item>

    <el-form-item>
      <el-button
        type="primary"
        class="fancy-btn confirm"
        @click="title === '添加广告' ? addadposition() : updateAdpostion()"
      >
        发布
      </el-button>
    </el-form-item>
    
  </el-form>
</el-drawer>


    </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}



</style>